import {
    reqDetailList,
    reqAddOrUpdateShopCart
} from '@/api'

const actions = {
    //给接口发请求，获取详情页数据
    async getDetailList({
        commit
    }, value) {
        let result = await reqDetailList(value);
        if (result.code == 200) commit('GET_DETAIL_LIST', result.data);
    },
    //添加购物车请求
    async getAddOrUpdateShopCart({
        commit
    }, {
        id,
        count
    }) {
        let result = await reqAddOrUpdateShopCart(id, count);
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    }
}

const mutations = {
    //详情页数据
    GET_DETAIL_LIST(state, value) {
        state.detailList = value;
    }
}

const getters = {
    //返回面包屑数据
    categoryView() {
        return state.detailList.categoryView || {};
    },
    //返回商品信息
    skuInfo() {
        return state.detailList.skuInfo || {};
    },
    //返回产品属性
    spuSaleAttrList() {
        return state.detailList.spuSaleAttrList;
    }
}

const state = {
    detailList: {}
}

export default {
    actions,
    mutations,
    getters,
    state
}